package gam.n1.conf.i;

import gam.n1.conf.BaseConfig;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;

public final class MsgConfig extends BaseConfig{
    private Properties p;
    private YamlConfiguration b;

    public MsgConfig(@NotNull Plugin plugin, @NotNull String filePath) {
        super(plugin, filePath);
    }

    @Override
    public boolean load(Plugin plugin) throws IOException {
        File confirm = null;
        File var1 = getBaseFile(plugin);
        if (var1.isDirectory()){
            String var2 = plugin.getConfig().getString("language");
            if (var2 != null){
                if (var1.list().length == 0){
                    plugin.saveResource(fP + "/en.properties",false);
                    confirm = new File(plugin.getDataFolder(),fP + "/en.properties");
                } else {
                    String[] var3 = var1.list();
                    if (var3 != null){
                        for (String var5 : var3) {
                            int i = var5.indexOf(".");
                            String name = var5.substring(0,i);
                            if (var2.contains(".")) {
                                if (var5.equals(var2)) {
                                    confirm = new File(plugin.getDataFolder(),fP + "/" + var5);
                                    break;
                                }
                            } else {
                                if (name.equals(var2)) {
                                    confirm = new File(plugin.getDataFolder(),fP + "/" + var5);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (confirm != null){
            if (confirm.getName().endsWith(".properties")){
                p = new Properties();
                p.load(new FileInputStream(confirm));
                System.out.println(p.get("area.create.error.asking"));
            } else if (confirm.getName().endsWith(".yml")){
                b = YamlConfiguration.loadConfiguration(confirm);
            }
            return true;
        }
        return false;
    }
    public String getString(String key){
        Objects.requireNonNull(key);
        if (p != null) return (String) p.getOrDefault(key,null);
        if (b != null) return b.getString(key);
        return null;
    }
    public void send(CommandSender sender,String msg,String ph,String value){
        Objects.requireNonNull(sender);
        Objects.requireNonNull(msg);
        String var1 = getString("area.prefix");
        if (var1 != null){
            msg = ph != null ? msg.replace(ph,value) : msg;
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&',var1 + msg));
        }
    }

    @Override
    public boolean reload(Plugin plugin) throws IOException {
        return load(plugin);
    }
}
